home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 19 / Mac Magazin and MacEasy Magazine CD - Issue 19.iso / Online / HttpServerƒ / •HTTP Server / THTTPServer.cp < prev    next >
Text File  |  1996-01-03  |  9KB  |  299 lines

  1. //    THTTPServer.cp - Macintosh OpenTransport Network HTTP Server class object
  2. // 
  3. // Apple Macintosh Developer Technical Support
  4. // Written by:  Vinne Moscaritolo
  5. //
  6. //  Copyright (work in progress)  Apple Computer, Inc All rights reserved.
  7. //
  8. // You may incorporate this sample code into your applications without
  9. // restriction, though the sample code has been provided "AS IS" and the
  10. // responsibility for its operation is 100% yours.  However, what you are
  11. // not permitted to do is to redistribute the source as "DSC Sample Code"
  12. // after having made changes. If you're going to re-distribute the source,
  13. // we require that you make it clear in the source that the code was
  14. // descended from Apple Sample Code, but that you've made changes.
  15. // 
  16.  
  17. #include <Resources.h>
  18. #include <OSUtils.h>
  19. #include <strstream.h>
  20. #include <iomanip.h>
  21.  
  22. #include "THTTPServer.h"
  23. #include "TGMT.h"
  24. #include "TAddrInet.h"
  25.  
  26.  
  27. //Get Flagship name
  28. Boolean  GetFlagshipName( char str[] );
  29. Boolean  GetFlagshipName( char str[] )
  30. {
  31.     Handle h ;    
  32.     if(h = GetResource('STR ',-16413));                // get flagship name
  33.     else if(h = GetResource('STR ',-16096));        // get user name
  34.     else { str[0] = '\0'; return false;}
  35.  
  36.     HLock(h);
  37.     strncpy(str,*h +1, **h);
  38.     str[**h] = '\0';
  39.     HUnlock(h);
  40.  
  41.     return true;
  42. }
  43.  
  44. // ---------------------------------------------------------------------------
  45. //     THTTPServerApp
  46. // ---------------------------------------------------------------------------
  47. //    Default Constructor
  48.  
  49. THTTPServerApp::THTTPServerApp()
  50. {
  51.         
  52.     GetFlagshipName(fDescriptor.GetServiceName());  // ***** fix this latter
  53.     fDescriptor.Unstream( "????" );
  54.  
  55.     fServer = new THTTPServer();
  56.     fServer->Open( &fDescriptor);
  57. }
  58.  
  59. // ---------------------------------------------------------------------------
  60. //     ~TNetworkServerApp
  61. // ---------------------------------------------------------------------------
  62. //    Destructor
  63.  
  64. THTTPServerApp::~THTTPServerApp()
  65. {
  66.     if(fServer) delete fServer;
  67. };
  68.  
  69. // ---------------------------------------------------------------------------
  70. //     THTTPServerApp::Start()
  71. // ---------------------------------------------------------------------------
  72. //    
  73.  
  74. void THTTPServerApp::Start()
  75. {
  76. // handle any specific startup issues.        
  77.  
  78.     TNetworkServerApp::Start();        // call the inherited Start
  79. }
  80.  
  81.  
  82. // ---------------------------------------------------------------------------
  83. //     THTTPServer
  84. // ---------------------------------------------------------------------------
  85. //    Default Constructor
  86.  
  87. THTTPServer::THTTPServer()
  88. {
  89. }
  90.  
  91. // ---------------------------------------------------------------------------
  92. //     ~THTTPServer
  93. // ---------------------------------------------------------------------------
  94. //    Destructor
  95.  
  96. THTTPServer::~THTTPServer()
  97. {
  98.  
  99. };
  100.  
  101. // ---------------------------------------------------------------------------
  102. //     THTTPServer:: Factory()
  103. // ---------------------------------------------------------------------------
  104. //    Factory
  105.  
  106. TNetworkSession* THTTPServer:: SessionFactory()
  107. {
  108.     return  new THTTPSession(this);
  109. }
  110.  
  111.  
  112. // ---------------------------------------------------------------------------
  113. //     THTTPSession
  114. // ---------------------------------------------------------------------------
  115. //    Default Constructor
  116.  
  117. THTTPSession::THTTPSession(THTTPServer* theServer): TNetworkSession(theServer)
  118. {
  119. }
  120.  
  121. // ---------------------------------------------------------------------------
  122. //     ~THTTPSession
  123. // ---------------------------------------------------------------------------
  124. //    Destructor
  125.  
  126. THTTPSession::~THTTPSession()
  127. {
  128.  
  129. };
  130.  
  131. void dumpInterfaceInfo (ostream &os, EndpointRef  ep);
  132. void dumpPortInfo(ostream &os, EndpointRef  ep);
  133.  
  134. #define CRLF "\r\n"
  135.  
  136.  
  137. // ---------------------------------------------------------------------------
  138. //    THTTPSession::Run
  139. // ---------------------------------------------------------------------------
  140. //    THTTP Session thread
  141.  
  142. void* THTTPSession::Run()
  143. {
  144.     TNetworkIOStream&  nio   = GetStreamRef();
  145.     TGmt        currentTime;
  146.     
  147.  
  148. // record time
  149.     currentTime.Now();
  150.      
  151.          
  152. // send Reply
  153.  
  154. // header
  155.      nio << "HTTP/1.0 200 OK" << CRLF;
  156.     nio << "MIME-Version: 1.0" << CRLF;
  157.     nio << "Content-type: text/html" << CRLF;
  158.     nio << "Date: " << currentTime << CRLF;
  159.     nio << "Server: Knucklehead /1.0" << CRLF;
  160.     nio <<  CRLF;
  161.  
  162. // body
  163.      TNetworkEndpointDescriptor* epd = fServer->GetEPD();
  164.     TAddrInet addr     =  &fCallInfo.addr ;
  165.     TAcceptorInfo stats;
  166.     
  167.     nio << "<HEAD>";
  168. //    nio << "<META HTTP-EQUIV=\"Refresh\" CONTENT=0>";
  169.     nio << "<TITLE>Test Page</TITLE></HEAD><BODY>";
  170.     nio << "<H2>Vinnie's OpenTransport HTTP Test Server</H2>";
  171.     
  172.     nio << "<Table BORDER=0 CELLSPACING=2 CELLPADDING=0><b>"; 
  173.     nio << "<b>Server Info: </b>"; 
  174.     nio << "<TR><TD><b>Name:</b></TD><TD>" << epd->GetServiceName() << "</TD></TR>";
  175.     nio << "<TR><TD><b>Date:</b></TD><TD>" << currentTime << "</TD></TR>";
  176.         if( fServer->GetStats(&stats)){
  177.         nio << "<TR><TD><b>Active Sessions:</b></TD><TD>" << stats.fActiveSessions << "</TD></TR>";
  178.         nio << "<TR><TD><b>Free:</b></TD><TD>" << stats.fFreeSessions << "</TD></TR>";
  179.         nio << "<TR><TD><b>Accepted:</b></TD><TD>" << stats.fAcceptedSessions << "</TD></TR>";
  180.         nio << "<TR><TD><b>Rejected:</b></TD><TD>" << stats.fRejectedSessions << "</TD></TR>";
  181.         nio << "<TR><TD><b>Up Time:</b></TD><TD>" << (int) (stats.fUpTime / 60) << " min </TD></TR>";
  182.         nio << "<TR><TD><b>Client:</b></TD><TD>" << addr << "</TD></TR>";
  183.         nio << "<TR><TD><b>StackSpace:</b></TD><TD>" << StackSpace() << "</TD></TR>";
  184.         
  185.         
  186.             };
  187.     nio << "</Table><p>" << flush;
  188.     
  189.      dumpInterfaceInfo(nio, fEndPoint);
  190.     nio << "</BODY>\r\n" << CRLF ;
  191.     nio << CRLF << flush ;
  192.  
  193.  
  194.     int cntr = 16;
  195.     while(cntr--) {
  196.              Yield();
  197.          }
  198.      
  199.      return nil;
  200. }
  201.  
  202.  
  203.  
  204.  
  205.          
  206.     static char* busNames[] =
  207.     {
  208.         "Unknown",
  209.         "MthrBrd",
  210.         "NuBus",
  211.         "PCI",
  212.         "GeoPort",
  213.         "PCMCIA",
  214.         "Bus6",
  215.         "Bus7",
  216.         "Bus8",
  217.         "Bus9",
  218.         "Bus10",
  219.         "Bus11",
  220.         "Bus12",
  221.         "Bus13",
  222.         "Bus14",
  223.         "Bus15"
  224.     };
  225.  
  226.     static char* MACtypes[] = {
  227.                 "<<unknown type(0)>>",
  228.                 "ADEV Devices",
  229.                 "MDEV Devices",
  230.                 "LocalTalk Devices",
  231.                 "IRTalk Devices",
  232.                 "TokenRing Devices",
  233.                 "ISDN Devices",
  234.                 "ATM Devices",
  235.                 "SMDS Devices",
  236.                 "Serial Devices",
  237.                 "Ethernet Devices",
  238.                 "SLIP Devices",
  239.                 "PPP Devices",
  240.                 "Modem Devices",
  241.                 "100MB Ethernet Devices",
  242.                 "FDDI Devices",
  243.                 "ATM LANE Devices",
  244.                 "ATM SNAP Devices"
  245.                 };
  246.  
  247. void dumpPortInfo(ostream &os, EndpointRef  ep)
  248. {
  249. //    OTPortRef            port;
  250.     OTPortRecord        portInfo;
  251.     int                    index = 0;
  252.     
  253. //    port = ::OTGetProviderPortRef(ep); // doesnt work yet
  254. //    if( ::OTFindPortByRef(&portInfo, port) ){    
  255.         os << "<TR></TR>";
  256.     if ( ::OTGetIndexedPort(&portInfo, index) ) {
  257.         UInt16 dev = ::OTGetDeviceTypeFromPortRef(portInfo.fRef);
  258.         UInt16 Bus = ::OTGetBusTypeFromPortRef(portInfo.fRef);
  259.         os << "<TR><TD><b>Port " << index << "</b></TD></TR>";
  260.         os << "<TR><TD>Name:</TD><TD>" << portInfo.fPortName << "</TD></TR>";
  261.         os << "<TR><TD>Bus:</TD><TD> " << MACtypes[dev] << "</TD></TR>";
  262.         os << "<TR><TD>Module:</TD><TD>" << portInfo.fModuleName << "</TD></TR>";
  263.         os << "<TR><TD>Bus:</TD><TD> " << busNames[Bus] << "</TD></TR>";
  264.         if( portInfo.fSlotID[0] ) os << "<TR><TD>SlotInfo:</TD><TD>" << portInfo.fSlotID << "</TD></TR>";
  265.         if( portInfo.fResourceInfo[0] )  os << "<TR><TD>ResourceInfo:</TD><TD>" << portInfo.fResourceInfo << "</TD></TR>";
  266.         os << "<TR><TD>Flags:</TD><TD>";
  267.             if( portInfo.fInfoFlags & kOTPortIsDLPI) os << "DLPI ";
  268.             if( portInfo.fInfoFlags & kOTPortIsTPI) os <<  "TPI ";
  269.             if( portInfo.fInfoFlags & kOTPortCanYield) os << "Can-Yield ";
  270.             if( portInfo.fInfoFlags & kOTPortIsSystemRegistered) os << "System  ";
  271.             if( portInfo.fInfoFlags & kOTPortIsAlias) os << "Alias  ";
  272.         os << "</TD></TR>";
  273.     };
  274.     
  275. }
  276. void dumpInterfaceInfo (ostream &os, EndpointRef  ep)
  277. {
  278. InetInterfaceInfo    info;
  279. int     index = 0;
  280. char     buf[32];
  281.  
  282.     while ( ! ::OTInetGetInterfaceInfo( &info, index) )
  283.         {
  284.             os << "<Table BORDER=0 CELLSPACING=2 CELLPADDING=0><b>"; 
  285.                 if(index == kDefaultInetInterface)  os <<  "Default"; else   os << "Provider: " << index;  os << "</b> ";
  286.              ::OTInetHostToString( info.fAddress, buf);                 os <<  "<TR><TD>Address:</TD><TD>" << buf << "</TD></TR>";
  287.              ::OTInetHostToString( info.fNetmask, buf);              os <<  "<TR><TD>NetMask:</TD><TD>" << buf << "</TD></TR>";
  288.              ::OTInetHostToString( info.fBroadcastAddr, buf);          os <<  "<TR><TD>Broadcast:</TD><TD>" << buf << "</TD></TR>";
  289.              ::OTInetHostToString( info.fDefaultGatewayAddr, buf);     os <<  "<TR><TD>Gateway:</TD><TD>" << buf << "</TD></TR>";
  290.              ::OTInetHostToString( info.fAddress, buf);              os <<  "<TR><TD>DNS:</TD><TD>" << buf << "</TD></TR>";
  291.              os << "<TR><TD>Domain:</TD><TD>" << info.fDomainName << "</TD></TR>";
  292.             dumpPortInfo(os,ep);
  293.              os << "</Table><p>";
  294.             index++;
  295.     };
  296. }
  297.  
  298.  
  299.